<?php
//统计轮询
class Acmd_stat{
	function  __construct() {
		$this->afile = Afunc_file();
		$this->path = A_ROOT.'/temp/stat/';
	}
	function arun($argv){
		switch ($argv[2]){
			case 'run':
				$this->statall();//运行
			//	Afun_cmd_echo('运行结束');
				break;
			case 'backup':
				$this->mysql_backup($argv[3]);
				break;
		}
	}


//--处理stat文件夹
	function statall(){
		$dirs = $this->afile->ascandiro($this->path);
		if(empty($dirs->dir))
			return true;
		foreach($dirs->dir as $key=>$val){
			$this->statday($dirs->path,$val);
		}
	}
//--处理日期文件夹
	function statday($path,$str){
		$dirs = $this->afile->ascandiro($path.$str.'/');
		if(empty($dirs->file))
			return true;
		foreach($dirs->file as $key=>$val){
			$this->statone($dirs->path,$str,$val);
		}
	}
//--处理时间文件
	function statone($path,$date,$val){
		$filetime = explode('_',$val);
		$filedate = $date.' '.str_replace('-',':',$filetime[0]);
		//echo strtotime($filedate);
		$filepath = $path.$val;
		$content = $this->afile->aedit_view($filepath);
		$content = explode('|',$content);
		$bot = $this->statip($content[1],$content[3]);
		if($bot < 0){
			$this->afile->aunlink($filepath);
			return true;
		}
		$ipr = Afun_ip_info($content[1]);
		$city = $ipr['city'];
		$citynet = $ipr['add'];
		$sql = "INSERT INTO `".Acan_db_pre."http` 
		(`ip`, `time`, `agent`, `host`, `url`, `referer`, `city`, `citynet`, `bot`)
		VALUES ('$content[1]', '$content[2]', '$content[3]', '$content[4]', '$content[5]', '$content[6]', '$city', '$citynet', '$bot')";
		Afun_db()->exec($sql);
		Afun_cmd_echo(date("Y-m-d H:i:s",$content[2]).'-'.$city.' - '.$citynet);
		$this->afile->aunlink($filepath);
	}

//--过滤IP
	function statip($ip,$agent){
		if(in_array($ip,$this->nosaveip())){
			$bot = -1;
		}else{
			$ip_num=Afun_ipnum($ip);
			if(preg_match ("/(spider|baidu|google|soso|sogou|yahoo)/i",$agent) || ($ip_num<=2087912715 && $ip_num>=2087911424)){
				$bot='2';
			}else if(in_array($ip,$this->selfip())){
				$bot='5';
			}else{
				$bot='1';
			}
		}
		return $bot;
	}
//--不保存的IP列表
	function nosaveip(){
		return array('127.0.0.1');
	}

//--不保存的IP列表
	function selfip(){
		return array('122.227.207.235');
	}










}

?>